import logging

from fastapi import FastAPI
from app.web.crontab.Scheduler import init_scheduled_tasks
from app.web.models.Database import init_database
from app.web.routers.Router import init_route
from fastapi.middleware.cors import CORSMiddleware

logger = logging.getLogger(__name__)

origins = [
    "http://localhost:5173"
]

# Allow these methods to be used
methods = ["GET", "POST", "PUT", "DELETE"]
# Only these headers are allowed
headers = ["Content-Type", "Authorization"]


def create_app():
    logger.info("Create App")
    webapp = FastAPI()
    webapp.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=methods,
        allow_headers=headers
    )
    init_route(webapp)
    init_database()
    init_scheduled_tasks()
    return webapp


app = create_app()
